<template>
  <el-card>
    <div>
      <!-- 1 查询的表单 -->
      <el-form
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
        size="mini"
      >
        <el-form-item label="订单号">
          <el-input
            v-model="formInline.orderNo"
            placeholder="订单号"
          ></el-input>
        </el-form-item>
        <el-form-item label="收货人">
          <el-input
            v-model="formInline.consignee"
            placeholder="收货人"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="formInline.phone" placeholder="手机号"></el-input>
        </el-form-item>
        <el-form-item label="订单状态">
          <el-select v-model="formInline.orderState" placeholder="订单状态">
            <el-option label="已受理" value="已受理"></el-option>
            <el-option label="派送中" value="派送中"></el-option>
            <el-option label="已完成" value="已完成"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择时间">
          <el-date-picker
            v-model="formInline.date"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
      <!-- 2 订单展示的表格 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="orderNo" label="订单号"> </el-table-column>
        <el-table-column label="下单时间">
          <template v-slot="{ row }">
            {{ row.orderTime | formateDate }}
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="手机号" width="120">
        </el-table-column>
        <el-table-column prop="consignee" label="收货人"> </el-table-column>
        <el-table-column prop="deliverAddress" label="配送地址" width="100">
        </el-table-column>
        <el-table-column label="送达时间">
          <template v-slot="{ row }">
            {{ row.deliveryTime | formateDate }}
          </template>
        </el-table-column>
        <el-table-column prop="remarks" label="用户备注"> </el-table-column>
        <el-table-column prop="orderAmount" label="订单金额"> </el-table-column>
        <el-table-column prop="orderState" label="订单状态"> </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template v-slot="{ row }">
            <el-button @click="handleClick(row)" type="text" size="small"
              >查看</el-button
            ><router-link :to="'/order/edit?id=' + row.id">
              <el-button type="text" size="small">编辑</el-button>
            </router-link>
          </template>
        </el-table-column>
      </el-table>
      <!-- 3 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <!-- 4 查看弹窗界面 -->
      <el-dialog title="提示" :visible.sync="dialogVisible" width="50%">
        <div>
          <p>{{ curItem.orderNo }}</p>

          <!-- 查看界面布局 -->
          <el-timeline :reverse="true">
            <el-timeline-item
              v-for="(activity, index) in activities"
              :key="index"
              :timestamp="activity.timestamp"
            >
              {{ activity.content }}
            </el-timeline-item>
          </el-timeline>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </el-card>
</template>

<script>
import { orderList } from '@/api/orders.js'
import moment from 'moment'
import _ from 'lodash'
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 5,
      total: 0,
      tableData: [],
      multipleSelection: [],
      formInline: {
        orderNo: '',
        consignee: '',
        phone: '',
        orderState: '',
        date: []
      },
      dialogVisible: false,
      curItem: {},
      activities: [
        {
          content: '活动按期开始',
          timestamp: '2018-04-15'
        },
        {
          content: '通过审核',
          timestamp: '2018-04-13'
        },
        {
          content: '创建成功',
          timestamp: '2018-04-11'
        }
      ]
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    handleClick(row) {
      this.curItem = _.cloneDeep(row)
      this.dialogVisible = true
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getOrderList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getOrderList()
    },
    async getOrderList() {
      const { data, total } = await orderList({
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        orderNo: this.formInline.orderNo,
        consignee: this.formInline.consignee,
        phone: this.formInline.phone,
        orderState: this.formInline.orderState,
        date: JSON.stringify(this.formInline.date)
      })
      this.tableData = data
      this.total = total
      console.log(data)
    },
    onSubmit() {
      // this.currentPage = 1
      this.getOrderList()
    }
  },
  filters: {
    formateDate(str) {
      return moment(str).format('HH:mm:ss')
    }
  }
}
</script>

<style lang="less" scoped>
.el-pagination {
  margin-top: 40px;
}
/deep/ .cell {
  text-align: center;
}
.el-form {
  margin-bottom: 20px;
}
/deep/ .el-date-editor--datetimerange.el-input,
.el-date-editor--datetimerange.el-input__inner {
  width: 300px;
}
.el-timeline {
  margin-top: 30px;
}
</style>
